﻿using Shop.BLL;
using Shop.Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Shop.MVC.Controllers
{
    // 购物车
    public class CartController : Controller
    {
        BaseManager<Cart> cartManager = new BaseManager<Cart>();
        // 加入购物车
        public ActionResult Add(Cart model)
        {
            // 判断用户是否登录
            if (Session["User"] == null)
            {
                //return Content("<script>alert('请先登录！');location.href='/account/Login'</script>");   // 此种方式只能应用于非ajax请求
                return Json(new { success = false, msg = "用户没有登录！" });
            }
            // 如果已登录，将数据添加到购物车表
            model.UserId = ((Users)Session["User"]).Id;
            cartManager.Add(model);
            return Json(new { success = true, msg = "已成功加入购物车！" });

        }

        // 显示购物车页面
        public ActionResult MyCart()
        {
            var user = Session["User"] as Users;
            if ( user==null)  // 没有登录
            {
                return RedirectToAction("login", "account");
            }
            var where = "userid=@uid";
            SqlParameter[] sp =
            {
                new SqlParameter("@uid",user.Id)
            };
            ViewBag.List= cartManager.GetList(where, sp);
            return View();
        }
    }
}